﻿--[[
   @brief    WGSelectZone(区服信息按钮)
   @author   harleyxwang
   @version  1.0
   @date     2017-04-27
--]]

ierd_tgp.tpf_ui.Require("common\\common.lua");
ierd_tgp.tpf_ui.Require("frame_bottom\\select_zone_item.lua");

MenuSate2Button = {
    get_button = function(expanded)
        if expanded then
            return "BtnZoneUp";
        else
            return "BtnZoneDown";
        end
    end
};

WGSelectZones = {};

WGSelectZone = {
    DELAY_CTN_HEIGHT = 20;
};

function WGSelectZone:new (game_id, uob_zone_id)
    ierd_tgp.tpf_ui.Log("WGSelectZone:new,  game_id:".. tostring(game_id) ..
        ", uob_zone_id:" .. tostring(uob_zone_id));
        
    local drop_id = ui_utility.get_ctrl_id_by_path(uob_zone_id, "CntDrop");
    ierd_tgp.tpf_ui.Log("WGSelectZone:new, got drop_id:".. tostring(drop_id));
    
    local o = {uilib_ = ui_utility.uilib(), game_id_ = game_id, ctrl_id_ = drop_id,
               enabled_ = true, state_ = 0, expanded_ = false, uob_zone_id_ = uob_zone_id};
    setmetatable(o, self);
    self.__index = self;
    o:init();
    WGSelectZones[game_id] = o;
    return o;
end

function WGSelectZone:init()   
    self.icons_id_ = ui_utility.get_ctrl_id_by_path(self.ctrl_id_, "CntIcons");

    self:show(false);
    self:set_expanded(false);
end

function WGSelectZone:log(msg, core_msg)
    base_utility.log(string.format("[WGSelect][%d][Zone] %s", self.game_id_, msg), core_msg);
end

function WGSelectZone:set_visible(visible)
    base_utility.log(string.format("[WGSelectZone][%d]set_visible %s", self.game_id_, tostring(visible)));
    MainCommon.set_visible(self.uilib_, self.ctrl_id_, visible);
end

function WGSelectZone:set_enable(enabled)
    base_utility.log(string.format("[WGSelectZone][%d]set_enable %s", self.game_id_, tostring(enabled)));
    if nil == enabled then
        return;
    end

    self.enabled_ = enabled;
    
    MainCommon.set_enabled(self.uilib_, self.ctrl_id_, "CntZoneButtons", self.enabled_);   
    MainCommon.set_enabled(self.uilib_, self.ctrl_id_, "CntZoneButtons\\BtnZoneDown", self.enabled_);  
    MainCommon.set_enabled(self.uilib_, self.ctrl_id_, "CntZoneButtons\\BtnZoneUp", self.enabled_);  
    MainCommon.set_enabled(self.uilib_, self.ctrl_id_, "BtnZoneName", self.enabled_);

    MainCommon.set_enabled(self.uilib_, self.icons_id_, self.icon_name_, self.enabled_);
end

function WGSelectZone:set_state(state)
    base_utility.log(string.format("[WGSelectZone][%d]set_state %d", self.game_id_, state));
    self.state_ = state;
    
    local icon_name = ZoneState2Icon.get_icon_name(state);
    
    -- 刷新图标及状态
    MainCommon.show_only_child(ui_utility.uilib(), self.icons_id_, icon_name);    
    self.icon_name_ = icon_name;
end

function WGSelectZone:set_zone_name(name)
    MainCommon.set_button_caption(ui_utility.uilib(), self.ctrl_id_, "BtnZoneName",
       name);   
end

function WGSelectZone:set_expanded(expanded)
    base_utility.log(string.format("[WGSelectZone][%d]set_expanded", self.game_id_));
    self.expanded_ = expanded;
    
    local button_name = MenuSate2Button.get_button(self.expanded_);
    local buttons_id = MainCommon.get_child_id(self.uilib_, self.ctrl_id_, "CntZoneButtons");
    MainCommon.show_only_child(self.uilib_, buttons_id, button_name);
    -- MainCommon.set_enabled(self.uilib_, buttons_id, button_name, self.enabled_);
end

function WGSelectZone:get_expanded()
    return self.expanded_;
end

function WGSelectZone:set_zone_info(server_info) 
    self:log("set_zone_info");
    if server_info == nil then
        self:log("[set_zone_info]server_info == nil", true);
        return false;
    end

    self:set_state(server_info.state);
    self:set_zone_name(server_info.name);
    
    self.selected_zone_id_ = server_info.id;

    local delay_id = ui_utility.get_ctrl_id_by_path(self.ctrl_id_,
        "delayTime");

    WGSelectZoneItem:set_delay(delay_id, self.game_id_,
        server_info.id, server_info.acce_info);
         
    return true;
end

function WGSelectZone:show(show)
    self:log("show, " .. tostring(show) .. ", uob_zone_id:" .. tostring(self.uob_zone_id_));
    ui_utility.show_ctrl(self.uob_zone_id_, show);
    self.is_visible_ = show;
end

function WGSelectZone:IsVisible()
    return self.is_visible_;
end
------------------------------------UIEventListener-----------------------------------------------------------------

WGSelectZoneUIEventListener = {
};

function WGSelectZoneUIEventListener.OnZoneListShow(event_id, json_str)
    base_utility.log("[BannerUIEventListener][OnZoneListShow]json_str:" .. tostring(json_str));
    local param = base_utility.json_to_table(json_str);
    if param == nil or param.game_id == nil or param.show == nil then
        base_utility.log("[BannerUIEventListener][OnZoneListShow]param INVALID, json_str:" ..
            tostring(json_str), true);
        return;
    end
    
    local the_banner_zone = WGSelectZones[param.game_id];
    if the_banner_zone == nil then
        base_utility.log("[BannerUIEventListener][OnZoneListShow]the_banner_zone is NIL, json_str:" ..
            tostring(json_str), true);
        return;
    end
    
    the_banner_zone:set_expanded(param.show);
end

function WGSelectZoneUIEventListener.on_btn_zone_down_click(UILib, dwControlID, x, y)
    base_utility.log(string.format("[BannerUIEventListener]on_btn_zone_click, ctrl_id:%d",
       dwControlID));

    local cnt_zone_btns_id = ui_utility.get_ctrl_parent_id(dwControlID);
    local cnt_drop_id = ui_utility.get_ctrl_parent_id(cnt_zone_btns_id);
    local uob_zone_id = ui_utility.get_ctrl_parent_id(cnt_drop_id);
    local banner_normal_id = ui_utility.get_ctrl_parent_id(uob_zone_id);  
    local banner_id = ui_utility.get_ctrl_parent_id(banner_normal_id);
    
    local banner = WGSelectList:get_banner_by_ctrl(banner_id);
    if banner == nil then
        base_utility.log(string.format("[WGSelectActionUIEventListener][on_btn_zone_down_click]banner is NIL," .. 
            " dwControlID:%d, got banner_id:%d",
            dwControlID, banner_id), true);
        return;
    end

    local game_id = banner.game_id_;

    local control_ptr = ui_utility.get_ctrl_ptr_by_id(banner_id);
    local rc = nil;
    if control_ptr ~= nil then
        rc = control_ptr:GetRectEx(TRUE,TRUE);
    end

    local json_param;

    if rc ~= nil then 
        local buttun_height = rc.bottom - rc.top;
        local xoffset = rc.left;
        local yoffset = rc.bottom;
        WGSelectZoneList.init(WGSelectZoneList.LIST_TYPE.FOR_MINI_MODE);
        WGSelectZoneList.show(xoffset, yoffset, game_id);
    end
end

function WGSelectZoneUIEventListener.on_btn_zone_up_click(UILib, dwControlID, x, y)
    base_utility.log(string.format("[WGSelectActionUIEventListener]on_btn_zone_click, ctrl_id:%d",
       dwControlID));

    -- TODO:BannerUIEventListener.hide_zone_list();
end
 
ZoneState2Icon.init();
event_center.add_listener(banner_event.ZONE_LIST_SHOW, "WGSelectZoneUIEventListener.OnZoneListShow");